package controllers.Account;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import util.GoEasyUtil;
import controllers.displayEntites.LoginDisplay;
import core.dao.LoginDisplayService;

@Controller
@RequestMapping("login")
public class LoginController {
	@Resource(name = "goEasyUtil")
	private GoEasyUtil goEasyUtil;

	@Resource(name = "loginDisplayServiceImpl")
	private LoginDisplayService loginDisplayServiceImpl;

	@RequestMapping("loginPage")
	public String loginPage(HttpServletRequest request) {
		// return "test/login";
		return "login/login";
	}

	// @ResponseBody
	// @RequestMapping(value = "login", method = RequestMethod.POST)
	// public LoginDisplay login(HttpServletRequest request,
	// @RequestBody LoginDisplay loginDisplay) throws Exception {
	// // 登录成功将用户的id md5码作为url{key}
	// LoginDisplay login = loginDisplayServiceImpl.getLogin(
	// loginDisplay.getNickName(), loginDisplay.getPassWord());
	// if (login != null) {
	// System.out.println(login.getEncryptionId());
	//
	// System.out.println(login.DecryptByDisplay());
	// checkOldSess_saveNewSess(login, request);
	// // 登录成功后设置登陆session和key
	// request.getSession().setAttribute("login", login);
	// request.setAttribute("key", login.getEncryptionId());
	// }
	// return login;
	// }
	// 表单提交无需使用requestBody 如果使用则会报451错
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(HttpServletRequest request, LoginDisplay loginDisplay)
			throws Exception {
		// 登录成功将用户的id md5码作为url{key}
		// LoginDisplay loginDisplay= new LoginDisplay();
		// loginDisplay.setNickName(nickName);
		// loginDisplay.setPassWord(passWord);
		LoginDisplay login = loginDisplayServiceImpl.getLogin(
				loginDisplay.getNickName(), loginDisplay.getPassWord());
		if (login != null) {
			// System.out.println(login.getEncryptionId());
			//
			// System.out.println(login.DecryptByDisplay());
			checkOldSess_saveNewSess(login, request);
			// 登陆成功则存放此时登陆的sesionID=session
			LoginSessionMap.SESSIONID_SESSION.put(request.getSession().getId(),
					request.getSession());
			request.setAttribute("sessionID", request.getSession().getId());
			return "play/play";
		}

		return "login/login";
	}

	// @RequestMapping(value = "/person/login", method = RequestMethod.POST)
	// public @ResponseBody
	// User login(@RequestBody User person) {
	// return person;
	// }
	@ResponseBody
	@RequestMapping(value = "login2", method = RequestMethod.POST)
	public String login2(HttpServletRequest request,
			@RequestBody LoginDisplay loginDisplay) {
		return "test";
	}

	/**
	 * 检查是否登录过如果登陆过则对旧的session设置提示有重复登陆
	 * 
	 * @param login
	 * @param request
	 */
	private void checkOldSess_saveNewSess(LoginDisplay login,
			HttpServletRequest request) {

		HttpSession oldLoginSession = LoginSessionMap.LOGIN_SESSION.get(login
				.DecryptByDisplay());

		if (oldLoginSession != null) {
			// 已经有过该session则重复登录
			// System.out.println(oldLoginSession == request.getSession());
			// System.out.println(oldLoginSession.equals(request.getSession()));
			// System.out.println("oldSessID" + oldLoginSession.getId()
			// + "newSessID" + request.getSession().getId());
			if (oldLoginSession.getId() == request.getSession().getId()) {
				request.setAttribute("LoginMsg", "您已经登陆,无需重复登陆");
			} else {
				// login.setMsg1("您已经登陆,无需重复登陆");
				if (oldLoginSession.getAttribute("login") != null) {
					// System.out.println("oldSessionID:"
					// + oldLoginSession.getId());
					// 对在别的浏览器上的上个登陆者设置已有其他人登陆您的账号提示信息和移除session中的登录者
					oldLoginSession.setAttribute("orderLoginMsg", "1");
					oldLoginSession.removeAttribute("login");

					// 使用实时推送GoEsy直接发送内容是發到所有客戶端
					// goEasyUtil.sendMsg("异地登录异常！！");
					// System.out.println("oldSessionKeyhhh:"
					// + ((LoginDisplay) oldLoginSession
					// .getAttribute("login")).getEncryptionId());
				}
			}

			// oldLoginSession.removeAttribute("login");
		}
		// 登录成功后设置登陆session和key
		request.getSession().setMaxInactiveInterval(20);
		request.getSession().setAttribute("login", login);
		System.out.println("SessionID:" + request.getSession().getId());
		System.out.println("newSessionKey:"
				+ ((LoginDisplay) request.getSession().getAttribute("login"))
						.getEncryptionId());
		request.setAttribute("key", login.getEncryptionId());
		LoginSessionMap.LOGIN_SESSION.put(login.DecryptByDisplay(),
				request.getSession());

	}

}